import 'dart:async';
import 'package:flutter/material.dart';

class TimerModel extends ChangeNotifier {
  late Timer timer;
  int _count = 10;///当前计数
  int get count => 10 - _count;///剩余时间

  _setCount(){
    _count++;
    notifyListeners();
  }

  startTimer(){
    _count = 0;
    const timeout = Duration(seconds: 1);
    Timer.periodic(timeout, (timer) { //callback function
      //1s 回调一次
      print('afterTimer='+DateTime.now().toString());
      _setCount();
      if (timer.tick == 10) {
        timer.cancel();
      }
    });
  }

  @override
  void dispose() {
    if(timer.isActive) {
      timer.cancel();
    }
    super.dispose();
  }
}